#!/usr/bin/env node

let buf1 = Buffer.alloc(256);
const log = console.log;
buf1[0]= 0;

log('buf1 length:',buf1.length);
log('buf1:',buf1);

//通过循环初始化buf1
for (let i=0;i<buf1.length;i++){
  buf1[i]=i;
}
log('\nnew buf1:',buf1);

//对buf1做切片处理
let buf2 = buf1.slice(246,256);
log('\nbuf2 length:',buf2.length);
log('buf2:',buf2);

//对buf2做填充处理，所有字节填充为0
buf2.fill(0);
log('\nnew buf2:',buf2);

//用数组初始化
let arr=['a','b','c',1,2,3];
let buf3=Buffer.from(arr);

log('\nbuf3:',buf3);
log('buf3 length:',buf3.length);

//用字符串初始化
let buf4 = Buffer.from('hello world','utf8');
log('\nbuf4:',buf4.length,buf4);

log(buf4.toString('utf8'));

//数据复制
buf4.copy(buf3,0,0,buf3.length);
log('\nbuf3:',buf3.length,buf3);
